#include "board.h"

volatile uint8_t uart_data = 0;
int main(void)
{
    //OLED字符串缓冲区
    char buff[100] = {0};
    delay_ms(1000);
    //TI库初始化
    SYSCFG_DL_init();

    //陀螺仪初始化
    jy901s_init();

    //编码器初始化
    encoder_init();
    stop_motor();

    // //蓝牙初始化
    // Bluetooth_Init();

    //定时器初始化
    timer_init();

    // //LCD初始化
    // lcd_init();

    //按键任务初始化
    user_button_init();

    //系统状态初始化
    sys_event_init();
    
    // LCD显示首页UI
    // ui_home_page();

    //OLED初始化
    OLED_Init();     
    OLED_Clear();

    //状态机初始化
	State_Machine_init();

    //串口屏幕初始化
    tjc_timer_init();
    // //发送数据到蓝牙
    // BLE_send_String((uint8_t *)"HELLO\n");

    gyro_pid_init();//定点转PID初始化
    angle_pid_init();//角度PID初始化
    tracing_pid_init();	//循迹pid初始化


    while (1) 
    {
        system_time++;
        // tjc_task();
        // tjc_PIDchange(&tracing_pid);
        tjc_UIpage();

        //按键任务确认
        if(task_state == 1)
        {
            remind_flag = 1; 
            State_Machine.Main_State = QUESTION_1;
            key_1_flag = 0;
            task_state = 0;
        }		
        if(task_state == 2)
        {
            remind_flag = 1; 
            State_Machine.Main_State = QUESTION_2;
            key_2_flag = 0;
            task_state = 0;
        }
        if(task_state == 3)
        {
            remind_flag = 1;  
            State_Machine.Main_State = QUESTION_3;
            key_3_flag = 0;
            task_state = 0;
        }
        if(task_state == 4)
        {
            remind_flag = 1;  
            State_Machine.Main_State = QUESTION_4;
            key_4_flag = 0;
            task_state = 0;
        }

        //状态机启动
        switch(State_Machine.Main_State)
        {
            case STOP_STATE:
                stop_motor();
                break;
            
            case QUESTION_1:
                Question_Task_1();
                break;
            
            case QUESTION_2:
                Question_Task_2();
                break;
            
            case QUESTION_3:
                Question_Task_3();				
                break;
            
            case QUESTION_4:
                Question_Task_4();	
            break;
                
            default:
                break;
        }	
        
        //无任务执行时显示当前测量数据
        if( system_time % 100 == 0 )// 1*100=100ms的任务
        {
            //刷新屏幕内容
            OLED_Refresh();
        }
        if( system_time % 50 == 0 )// 1*10=50ms的任务
        {
            //显示角度
            sprintf(buff,"angle = %.2f  ",get_angle());
            OLED_ShowString(0,16,(uint8_t *)buff,12,1);
            
            //显示里程数
            sprintf(buff,"odo = %d  ",key_1_flag);
            OLED_ShowString(0,32,(uint8_t *)buff,12,1);

            //显示循迹偏差数
            sprintf(buff,"black = %d  ",tracing_Digital());
            OLED_ShowString(0,48,(uint8_t *)buff,12,1);
        }
    }
}
// 串口的中断服务函数
void UART_0_INST_IRQHandler(void)
{
	// 如果产生了串口中断
	switch (DL_UART_Main_getPendingInterrupt(UART_0_INST))
	{
	case DL_UART_MAIN_IIDX_RX: // 如果是接收中断
		// 接发送过来的数据保存在变量中
		uart_data = DL_UART_Main_receiveData(UART_0_INST);
		// 将保存的数据再发送出去
		// uart0_send_char(uart_data);
		writeRingBuff(uart_data);
        // printf("data=%d\r\n",uart_data);
		break;

	default: // 其他的串口中断
		break;
	}
}